import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';

import 'package:flutter/material.dart';
import 'package:flutter_web_view/widget/f_webview/webview_controller.dart';
import 'package:flutter_web_view/widget/f_webview/webview_strategy.dart';
import 'package:flutter_webview_pro/webview_flutter.dart';

/// webview_flutter_pro 实现
class FlutterWebViewProStrategy extends StatefulWidget implements WebViewStrategy {
  const FlutterWebViewProStrategy({
    Key? key,
    required this.initialUrl,
    this.onTitleChanged,
    this.onWebViewCreated,
    this.onProgressChanged,
    this.onLoadStop,
  }) : super(key: key);

  @override
  State createState() {
    return FlutterWebViewProStrategyState();
  }

  @override
  final String initialUrl;

  @override
  final void Function(FWebViewController controller, String? url)?
      onTitleChanged;

  @override
  final void Function(FWebViewController controller)? onWebViewCreated;

  @override
  final void Function(FWebViewController controller, int progress)?
      onProgressChanged;

  @override
  final void Function(FWebViewController controller, String? url)? onLoadStop;
}

class FlutterWebViewProStrategyState extends State<FlutterWebViewProStrategy>
    implements FWebViewController {
  WebViewController? _controller;
  String _url = "";

  @override
  void initState() {
    super.initState();
    if (!kIsWeb) {
      if (Platform.isAndroid) {
        WebView.platform = SurfaceAndroidWebView();
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return WebView(
      initialUrl: widget.initialUrl,
      javascriptMode: JavascriptMode.unrestricted,
      onWebViewCreated: (WebViewController webViewController) {
        _controller = webViewController;
        if (widget.onWebViewCreated != null) {
          widget.onWebViewCreated!(this);
        }
      },
      onPageStarted: (String url) {
        setState(() {
          _url = url;
        });
      },
      onPageFinished: (String url) {
        String loadUrl = url.toString();
        if (widget.onLoadStop != null) {
          widget.onLoadStop!(this, loadUrl);
        }
        if (_controller != null) {
          _controller!.getTitle().then((title) => title ?? "").then((title) {
            if (title.isNotEmpty) {
              if (widget.onTitleChanged != null) {
                widget.onTitleChanged!(this, title);
              }
            }
          });
        }
      },
      onProgress: (int progress) {
        if (widget.onProgressChanged != null) {
          widget.onProgressChanged!(this, progress);
        }
      },
    );
  }

  @override
  Future<bool> canGoBack() {
    if (_controller == null) {
      return Future.sync(() => false);
    }
    return _controller!.canGoBack();
  }

  @override
  Future<bool> canGoForward() {
    if (_controller == null) {
      return Future.sync(() => false);
    }
    return _controller!.canGoForward();
  }

  @override
  Future<void> goBack() {
    if (_controller == null) {
      return Future.sync(() => null);
    }
    return _controller!.goBack();
  }

  @override
  Future<void> goForward() {
    if (_controller == null) {
      return Future.sync(() => null);
    }
    return _controller!.goForward();
  }

  @override
  Future<String?> getUrl() {
    if (_controller == null) {
      return Future.sync(() => null);
    }
    return Future(() => _url);
  }

  @override
  Future<String?> getTitle() {
    if (_controller == null) {
      return Future.sync(() => null);
    }
    return _controller!.getTitle();
  }

  @override
  Future<void> loadUrl(String url) {
    if (_controller == null) {
      return Future.sync(() => null);
    }
    return _controller!.loadUrl(url);
  }

  @override
  Future<void> reload() {
    if (_controller == null) {
      return Future.sync(() => null);
    }
    return _controller!.reload();
  }
}
